﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Xml.Linq;
using HigLabo.Net;
using HigLabo.Net.Extensions;

namespace HigLabo.Net.Twitter
{
    /// <summary>
    /// 
    /// </summary>
    public partial class TwitterClient
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        private String GetResultText(String url)
        {
            return this.GetResultText(HttpMethodName.Get, url, new TwitterEmptyCommand());
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="command"></param>
        /// <returns></returns>
        private String GetResultText(String url, TwitterCommand command)
        {
            return this.GetResultText(HttpMethodName.Get, url, command);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="methodName"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        private String GetResultText(HttpMethodName methodName, String url)
        {
            return this.GetResultText(methodName, url, new TwitterEmptyCommand());
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="methodName"></param>
        /// <param name="url"></param>
        /// <param name="command"></param>
        /// <returns></returns>
        private String GetResultText(HttpMethodName methodName, String url, TwitterCommand command)
        {
            OAuth1Client cl = this;
            String u = this.Protocol.ToString().ToLower() + "://" + url;
            IDictionary<String, String> d = new Dictionary<string, string>();

            if (methodName == HttpMethodName.Put ||
                methodName == HttpMethodName.Delete)
            { throw new ArgumentException(); }

            if (command != null)
            {
                d = command.GetParameters();
            }
            var res = cl.GetResponse(methodName, u, this.AccessToken.Token, this.AccessToken.TokenSecret, d);
            if (res.StatusCode != HttpStatusCode.OK
                && res.StatusCode != HttpStatusCode.Created
                && res.StatusCode != HttpStatusCode.Accepted)
            {
                throw new HttpResponseException(res);                
            }
            return res.BodyText;
        }
    }
}
